package com.yisu.expressway.manager.website.controller.orderMoudle;

import com.alibaba.fastjson.JSON;
import com.yisu.expressway.ewf.common.utils.HTTPClientUtil;
import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.RefundRecord;
import com.yisu.expressway.manager.core.service.IRefundRecordService;
import com.yisu.expressway.manager.domain.config.SystemConfig;
import com.yisu.expressway.manager.domain.request.RefundRecordRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.FAILURE;

/**
 * /**
 * User: 赵杰
 * Date: 2017/5/16
 * Time: 13:36
 * Desc: 退款业务控制流程.
 */
@Controller
@RequestMapping(value = "/refund")
public class RefundController {
    @Resource
    private IRefundRecordService iRefundRecordService;
    @Resource
    private SystemConfig systemConfig;

    @RequestMapping(value = "/index.htm")
    public String listPage(Model model, RefundRecordRequest refundRecordRequest) {
        try {
            if (!StringUtils.isEmpty(refundRecordRequest.getKeyword())) {
                refundRecordRequest.setKeyword(java.net.URLDecoder.decode(refundRecordRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<RefundRecord> RefundRecords = iRefundRecordService.getRefundRecordListByPage(refundRecordRequest);
            model.addAttribute("refundRecordVos", RefundRecords.getRows());
            model.addAttribute("currentPage", refundRecordRequest.getPage());
            model.addAttribute("totalPages", (refundRecordRequest.getTotalRecords() / refundRecordRequest.getRows()) + 1);
            model.addAttribute("keyword", refundRecordRequest.getKeyword());
            model.addAttribute("titleName", "订单退款申请列表");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "/orderModule/refundManager/index";
    }


    /**
     * 退款操作
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/refundAction.json")
    @ResponseBody
    public JsonResult refundAction(Model model, RefundRecord refundRecord) {
        try {
            if (refundRecord.getId() == null || refundRecord.getOrderId() == null) {
                return new JsonResult(FAILURE.code, "信息不合法！");
            }
            Map<String, String> param = new HashMap<>();
            param.put("orderId", String.valueOf(refundRecord.getOrderId()));
            param.put("orderRefundId", String.valueOf(refundRecord.getId()));
            String json = HTTPClientUtil.doPost(systemConfig.getRefundUrl(), param);
            JsonResult jsonResult = JSON.parseObject(json, JsonResult.class);
            return jsonResult;
        } catch (Exception ex) {
            return new JsonResult(FAILURE.code, "网络君泡妞去了！");
        }
    }


    /**
     * 退款操作
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/refundByHandAction.json")
    @ResponseBody
    public JsonResult refundByHandAction(Model model, RefundRecord refundRecord) {
        try {
            if (refundRecord.getId() == null || refundRecord.getOrderId() == null) {
                return new JsonResult(FAILURE.code, "信息不合法！");
            }
            RefundRecord updateModel = new RefundRecord();
            updateModel.setOrderId(refundRecord.getOrderId());
            updateModel.setId(refundRecord.getId());
            updateModel.setStatus(RefundRecord.StatusEnum.SUCCESS.code);
            updateModel.setRefundObjId("手动退款：自行查询");
            updateModel.setRefundNo("手动退款：自行查询");
            updateModel.setRefundTime(new java.sql.Timestamp(new java.util.Date().getTime()));
            if (iRefundRecordService.refundSuccess(updateModel)) {
                return new JsonResult();
            } else {
                return new JsonResult(FAILURE.code, "网络君泡妞去了！");
            }
        } catch (Exception ex) {
            return new JsonResult(FAILURE.code, "网络君泡妞去了！");
        }
    }

}
